package cn.qiyu5522.service.impl;

import cn.qiyu5522.constant.SystemConstants;
import cn.qiyu5522.domain.DTO.CommentDTO;
import cn.qiyu5522.domain.VO.CommentVO;
import cn.qiyu5522.domain.VO.PageVO;
import cn.qiyu5522.domain.entity.Comment;
import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.mapper.CommentMapper;
import cn.qiyu5522.service.CommentService;
import cn.qiyu5522.service.UserService;
import cn.qiyu5522.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-13
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private UserService userService;

    @Override
    public ResponseResult commentList(Long articleId, Integer pageNum, Integer pageSize,String commentType) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        // 根据文章ID 查询 该文章下的所有根评论
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId, articleId);
        // RootID=-1 为根评论
        queryWrapper.eq(Comment::getRootId, SystemConstants.ROOT_ID);
        // 根据 type 查询评论类别
        queryWrapper.eq(Comment::getType,commentType);
        // 根评论按创建时间降序排序
        queryWrapper.orderByDesc(Comment::getCreateTime);
        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        List<Comment> records = page.getRecords();


        // 转VO,相应属性赋值
        List<CommentVO> commentVOS = toCommentList(records);

        // 查询子评论
        List<CommentVO> result = commentVOS.stream().peek(commentVO -> {
           List<CommentVO> children = getChildren(commentVO.getId());
           commentVO.setChildren(children);

        }).collect(Collectors.toList());

        PageVO pageVO = new PageVO(result,page.getTotal());
        return ResponseResult.okResult(pageVO);
    }

    @Override
    public ResponseResult addComment(CommentDTO commentDTO) {
        // 转实体类
        Comment comment = BeanCopyUtils.copyBean(commentDTO, Comment.class);
        save(comment);
        return ResponseResult.okResult();
    }




    /**
     * 通过 rootId 获取 子评论
     * @param id
     * @return
     */
    private List<CommentVO> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        // 按创建时间升序排序
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);
        // 转VO， 并给子评论的username, toCommentUserName属性赋值
        List<CommentVO> commentList = toCommentList(list);
        return commentList;
    }

    /**
     * description: 转为VO对象，同时给username, toCommentUserName 属性赋值
     * @param comments
     * @return
     */
    private List<CommentVO> toCommentList(List<Comment> comments){
        List<CommentVO> commentVOS = BeanCopyUtils.copyBeanList(comments,CommentVO.class);
        commentVOS = commentVOS.stream().peek(
                o -> {
                    if (o.getToCommentUserId() != SystemConstants.TO_COMMENT_USER_ID) {
                        // 根据toCommentUserId 查询 该用户评论对象的nickname
                        o.setToCommentUserName(userService.getById(o.getToCommentUserId()).getNickName());
                        // 根据 createBy 查询该用户的nickname
                        o.setUsername(userService.getById(o.getCreateBy()).getNickName());
                    }
                }
        ).collect(Collectors.toList());
        return commentVOS;
    }

}
